//
//  FFBankcardAddVM.m
//  LLWallet
//
//  Created by tt on 2020/9/14.
//

#import "FFBankcardAddVM.h"
#import "FFBankcardAddInputCell.h"
#import "FFBankcardAddNextVC.h"


@interface FFBankcardAddVM ()



@end

@implementation FFBankcardAddVM


static FFBankcardAddVM *_instance = nil;

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone ];
    });
    return _instance;
}

+(instancetype) shared{
    if (_instance == nil) {
        _instance = [[super alloc]init];
    }
    return _instance;
}

- (NSArray<NSDictionary *> *)tableSectionMaps {
    
    __weak typeof(self) weakSelf = self;
    NSMutableArray *items = [NSMutableArray array];
    if(self.nameAuthResp) {
        [items addObject:@{
               @"class": FFBankcardAddInputModel.class,
               @"label": LLang(@"持卡人"),
               @"placeholder":LLang(@"持卡人姓名"),
               @"value": self.nameAuthResp.name?:@"",
               @"disable": @(true),
               @"showBottomLine": @(true),
        }];
    }
    [items addObject: @{
        @"class": FFBankcardAddInputModel.class,
        @"label": LLang(@"卡号"),
        @"placeholder":LLang(@"持卡人本人银行卡号"),
        @"keyboardType":@(UIKeyboardTypeNumberPad),
        @"isBankcard":@(true),
        @"value": self.bankcard?:@"",
        @"maxLen":@(24),
        @"onChange":^(NSString *value){
            weakSelf.bankcard = value;
        }
    }];
    
    return @[
        @{
            @"height":@(20.0f),
            @"items":items,
                        
        },
        @{
            @"height": @(40.0f),
            @"items": @[
                    @{
                        @"class": WKButtonItemModel2.class,
                        @"title": LLang(@"下一步"),
                        @"onPressed":^{
                            weakSelf.bankcard = [weakSelf.bankcard stringByReplacingOccurrencesOfString:@" " withString:@""];
                            if(weakSelf.bankcard.length<=0) {
                                [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLang(@"银行卡不能为空！")];
                                return;
                            }
                            // 检查银行卡是否有效
                            [[WKNavigationManager shared].topViewController.view showHUD];
                            [weakSelf getBankcardCheck].then(^(FFBankcardCheckResp *resp){
                                 [[WKNavigationManager shared].topViewController.view hideHud];
                                weakSelf.bankcardCheck = resp;
                                if(!resp.vaild) {
                                    [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLang(@"银行卡无效！")];
                                    return;
                                }
                                [[WKNavigationManager shared] pushViewController:[FFBankcardAddNextVC new] animated:YES];
                            }).catch(^(NSError *error){
                                [[WKNavigationManager shared].topViewController.view switchHUDError:error.domain];
                            });
                        },
                    }
            
            ],
        }
    ];
}

-(AnyPromise*) getNameAuthInfo {
  return  [[WKAPIClient sharedClient] GET:@"impay/nameauth" parameters:nil].then(^(NSDictionary *resultDict){
        if([resultDict[@"is_auth"] boolValue] && resultDict[@"data"]) {
            self.nameAuthResp = [FFNameAuthResp new];
            self.nameAuthResp.certType = resultDict[@"data"][@"cert_type"]?:@"";
            self.nameAuthResp.certNo = resultDict[@"data"][@"cert_no"]?:@"";
            self.nameAuthResp.name = resultDict[@"data"][@"name"]?:@"";
        }
        
        
    });
}

-(AnyPromise*) getBankcardCheck{
    return [[WKAPIClient sharedClient] GET:@"impay/bankcard/info" parameters:@{@"bankcard":self.bankcard?:@""} model:FFBankcardCheckResp.class];
}

-(void) clear {
    self.nameAuthResp = nil;
    self.bankcardCheck = nil;
    self.bankcard = @"";
}

@end



@implementation FFNameAuthResp

@end

@implementation FFBankcardCheckResp

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    FFBankcardCheckResp *resp = [FFBankcardCheckResp new];
    resp.vaild = [dictory[@"vaild"] boolValue];
    
    if(resp.vaild) {
        resp.data = (FFBankcardInfoResp*)[FFBankcardInfoResp fromMap:dictory[@"data"] type:ModelMapTypeAPI];
    }
    
    return resp;
}

@end

@implementation FFBankcardInfoResp

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    FFBankcardInfoResp *resp = [FFBankcardInfoResp new];
    resp.bankcardType = dictory[@"bankcard_type"];
    resp.bankcode = dictory[@"bankcode"];
    resp.bankname = dictory[@"bankname"];
    return resp;
}

@end
